package xyz.yuzegod.megawalls.timer;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.game.Game;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.game.GameState;
import xyz.yuzegod.megawalls.game.GameTeam;
import xyz.yuzegod.megawalls.stats.CurrencyPackage;
import xyz.yuzegod.megawalls.stats.EffectStatsContainer;
import xyz.yuzegod.megawalls.stats.KitStatsContainer;
import xyz.yuzegod.megawalls.stats.PlayerStats;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class GameOverTimer implements Runnable
{
    private Game game;
    private GameTeam winner;
    private int counter;
    private long endTime;
    
    public GameOverTimer(final Game game) {
        this.counter = 15;
        (this.game = game).setState(GameState.STOP);
        game.setGameOverTimer(this);
        this.endTime = System.currentTimeMillis();
        this.winner = game.getWinner();
        if (this.winner != null) {
            final List<String> messages = new ArrayList<String>();
            messages.add("§a▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬");
            messages.add("§f                                       §l超级战墙");
            messages.add(" ");
            messages.add("                                   §e§l胜出队伍 §7- " + this.winner.getTeamColor().getChatColor() + this.winner.getTeamColor().getText() + "队");
            messages.add("                               §7存活玩家的最终击杀数");
            messages.add(" ");
            int i = 0;
            for (Iterator<GamePlayer> it = GamePlayer.sortFinalKills().iterator(); it.hasNext() && i < 3; ++i) {
                final GamePlayer gamePlayer = it.next();
                messages.add("                               §7" + gamePlayer.getName() + " - §e" + gamePlayer.getFinalKills() + " 最终击杀");
            }
            messages.add(" ");
            messages.add("§a▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬");
            for (final String line : messages) {
                Bukkit.broadcastMessage(line);
            }
        }
    }
    
    @Override
    public void run() {
        if (this.counter == 15 && this.winner != null) {
            for (final GamePlayer gamePlayer : this.winner.getGamePlayers()) {
                gamePlayer.sendTitle("§6胜利！", "", 0, 200, 0);
                final PlayerStats stats = gamePlayer.getPlayerStats();
                stats.addWins();
                stats.addGames();
                stats.giveCoins(new CurrencyPackage(100, "(参与游戏)"));
                stats.giveCoins(new CurrencyPackage(180, "(胜利！)"));
                final KitStatsContainer kitStats = stats.getKitStats(stats.getSelected());
                kitStats.addWins();
                kitStats.addGames();
                kitStats.addPlayTime(this.endTime - this.game.getStartTime());
            }
            final List<GamePlayer> otherList = GamePlayer.getGamePlayers();
            otherList.removeAll(this.winner.getGamePlayers());
            for (final GamePlayer gamePlayer2 : otherList) {
                final EffectStatsContainer effectStats = gamePlayer2.getPlayerStats().getEffectStats();
                gamePlayer2.sendTitle("§7失败！", "§f胜出队伍§7- " + effectStats.getColor(this.winner.getTeamColor()).getChatColor() + this.winner.getTeamColor().getText() + "队", 0, 60, 0);
                final PlayerStats stats2 = gamePlayer2.getPlayerStats();
                stats2.giveCoins(new CurrencyPackage(100, "(参与游戏)"));
            }
        }
        else if (this.counter == 5) {
            for (final Player p : Bukkit.getOnlinePlayers()) {
                MegaWalls.getInstance().tpToLobby(p);
            }
        }
        else if (this.counter == 0) {
            Bukkit.shutdown();
        }
        --this.counter;
    }
}
